<?php
/**
 * Created by PhpStrom
 * User: 郭碧云
 * Date: 2021/7/12 17:34
 * Desc:
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\BaseAdminController;
use App\Lib\Logger;
use App\Service\QuestionService;
use App\Service\QuestionTypeService;

class QuestionTypeController extends BaseAdminController
{
    protected $questionTypeService = "";
    protected $questionService = "";
    public function _init() {
        $this->questionTypeService=new QuestionTypeService();
        $this->questionService=new QuestionService();
    }

    /**
     * 试题分类.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = $this->questionTypeService->getList("*",[],'',["id"=>"asc"],12);
        return view("admin.question_type.index",["types"=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if($this->request->isMethod("GET")){
            return view("admin.question_type.create");
        }else{
            $data = $this->request->all()["data"];
            $this->validateData($data);
            return $this->store($data);
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    private function store($data)
    {
        $data["site_id"]=getCurrentSiteId();
        $result = $this->questionTypeService->create($data);
        if(!empty($result)){
            $this->log(Logger::LOG_LEVEL_INFO,Logger::QUESTIONTYPE,"typeCreate","新增分类成功",$data);
            return $this->success([],"添加成功");
        }
        $this->log(Logger::LOG_LEVEL_ERROR,Logger::QUESTIONTYPE,"typeCreate","新增分类失败",$data);
        return $this->error(70000,"添加失败");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $this->validateData(["id"=>$id],'checkId');
        if($this->request->isMethod("GET")){
            $info = $this->questionTypeService->first(["id"=>$id]);
            return view("admin.question_type.edit",["info"=>$info]);
        }else{
            $data = $this->request->all()["data"];
            $this->validateData($data,'update');
            $checkName = $this->questionTypeService->hasName($id,$data);
            if($checkName){
                return $this->update($id,$data);
            }
            return $this->error(80002,"题型已存在");
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    private function update($id,$data)
    {
        $result = $this->questionTypeService->update($id,$data);
        if($result){
            $this->log(Logger::LOG_LEVEL_INFO,Logger::QUESTIONTYPE,"update","更新题型{$id}成功",$data);
            return $this->success([],"更新成功");
        }
        $this->log(Logger::LOG_LEVEL_ERROR,Logger::QUESTIONTYPE,"update","更新题型{$id}失败",$data);
        return $this->error(80000,"更新失败");
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->validateData(["id"=>$id],'checkId');
        //查看题型下是否有题,无题删除
        $hasQuestion=$this->questionService->first(["type_id"=>$id]);
        if(empty($hasQuestion)){
            $result = $this->questionTypeService->destroy($id);
            if (!$result) {
                $this->log(Logger::LOG_LEVEL_ERROR, Logger::QUESTIONTYPE, 'destroy', "分类{$id}删除失败", ['del_id' => $id]);
                return $this->error(80001, "删除失败");
            }
            return $this->layuiDataSuccess("", 0, "删除成功");
        }
        return $this->layuiDataError(80002, "请删除该题型下的试题后删除");

    }
}
